#include <WinTen.h>
#include <Windows.h>
#include <extend.h>
#include <Info-unz.h>
#include <FwMsgs.h>

#define FN_UNZIP     15000       // In WinApi.ch also

static far TZipRec zipFile;

//----------------------------------------------------------------------------//

static LPSTR Byte2Str( BYTE b, LPSTR szResult )
{
   szResult[ 1 ] = ( b % 10 ) + '0';
   szResult[ 0 ] = ( b /= 10 ) + '0';
   szResult[ 2 ] = 0;

   return szResult;
}

//----------------------------------------------------------------------------//

static LPSTR Word2Str( WORD wNumber, LPSTR szResult )
{
   WORD w = 4;

   szResult[ 0 ] = '0';
   szResult[ 1 ] = '0';
   szResult[ 2 ] = '0';
   szResult[ 3 ] = '0';
   szResult[ 4 ] = 0;

   do
   {
      szResult[ --w ] = ( wNumber % 10 ) + '0';
      wNumber /= 10;
   } while( wNumber );

   return szResult;
}

//----------------------------------------------------------------------------//

CLIPPER GETFIRSTIN() // ZIP( cZipName ) --> aFileInfo
{
   LPSTR szDate = "yyyymmdd";
   LPSTR szTime = "00:00";

   _bset( ( LPBYTE ) &zipFile, 0, sizeof( TZipRec ) );

   _reta( 9 );
   _storni( GetFirstInZip( _parc( 1 ), &zipFile ), -1, 1 );
   _storc( zipFile.szFileName, -1, 2 );

   Word2Str( ( zipFile.wDate >> 9 ) + 1980, szDate );
   Byte2Str( zipFile.wDate >> 5 & 15, szDate + 4 );
   Byte2Str( zipFile.wDate & 31,  szDate + 6 );
   _stords( szDate, -1, 3 );

   Byte2Str( zipFile.wTime >> 11, szTime );
   szTime[ 2 ] = ':';
   Byte2Str( zipFile.wTime >> 5 & 63, szTime + 3 );
   _storc( szTime, -1, 4 );

   _stornl( zipFile.lSize, -1, 5 );
   _stornl( zipFile.lCompressSize, -1, 6 );
   _stornl( zipFile.lHeaderOffset, -1, 7 );
   _storni( zipFile.wPackMethod, -1, 8 );
   _storni( zipFile.bAttr, -1, 9 );
}

//----------------------------------------------------------------------------//

CLIPPER GETNEXTINZ() // IP() --> aFileInfo
{
   LPSTR szDate = "yyyymmdd";
   LPSTR szTime = "00:00";

   _reta( 9 );
   _storni( GetNextInZip( &zipFile ), -1, 1 );
   _storc( zipFile.szFileName, -1, 2 );

   Word2Str( ( zipFile.wDate >> 9 ) + 1980, szDate );
   Byte2Str( zipFile.wDate >> 5 & 15, szDate + 4 );
   Byte2Str( zipFile.wDate & 31,  szDate + 6 );
   _stords( szDate, -1, 3 );

   Byte2Str( zipFile.wTime >> 11, szTime );
   szTime[ 2 ] = ':';
   Byte2Str( zipFile.wTime >> 5 & 63, szTime + 3 );
   _storc( szTime, -1, 4 );

   _stornl( zipFile.lSize, -1, 5 );
   _stornl( zipFile.lCompressSize, -1, 6 );
   _stornl( zipFile.lHeaderOffset, -1, 7 );
   _storni( zipFile.wPackMethod, -1, 8 );
   _storni( zipFile.bAttr, -1, 9 );
}

//----------------------------------------------------------------------------//

CLIPPER CLOSEZIPFI() // LE() --> nil
{
   CloseZipFile( &zipFile );
}

//----------------------------------------------------------------------------//

CLIPPER ISZIP() // ( cZipFile ) --> lSuccess
{
   _retl( IsZip( _parc( 1 ) ) );
}

//----------------------------------------------------------------------------//

CLIPPER UNZIPFILE() // cZipFileName, cOutFileName, nOffset, hWndAction, nAction -->
                    // nRetCode
{
   _retni( UnzipFile( _parc( 1 ), _parc( 2 ), _parnl( 3 ), ( HWND ) _parnl( 4 ),
                      IF( PCOUNT() > 4, _parni( 5 ), FN_UNZIP ) ) );
}

//----------------------------------------------------------------------------//
